import LayVmodelBase from "./LayVmodelBase.js"
import { logger, tools, runtime } from "./tools.js";



class LayTransferVmodel extends LayVmodelBase {
    config = null;
    instance = null; //tr

    constructor(elem, config) {
        super(elem);
        this.config = config || {};
        this.config.elem = this.element;
        this.transDirection = "toValue"; //仅支持 从组件到vm的单向绑定
    }

    /**[override] */
    viewModelInit() {
        super.viewModelInit();
        this.vm.value = ko.observableArray([]);
    }

    /**[override] */
    onDisabledChange(disabled) {
        //该组件不支持禁用        
        return;
    }

    /**[override] */
    init() {
        if(!super.init()) return;        
        let tis = this;
        let vm = this.vm;
        let defaultValue = this.config.value || [];
        let transfer = tools.laymod('transfer');
        if (!this.config.id) {
            this.config.id = tools.cmpId("transfer");
        }

        //避免覆盖原有事件
        var innerChange = this.config.change;
        this.config.onchange = function (value, index) {
            //只处理右侧列表的变化
            let selds = transfer.getData(tis.config.id).map((item) => item.value);            
            vm.value(selds);            
            innerChange && innerChange.call(tis, ...arguments);
        }
        vm.value(defaultValue);
        transfer.render(this.config);
        this.inited = true;
    }

}


export default LayTransferVmodel;